# 原文件路径
file_path = r"D:\subtitle\bring_goods\txt\subtitle17.txt"

# 目标文件路径前缀
file_prefix = r"D:\subtitle\bring_goods\txt\subtitle17_part"

# 目标大小（6MB）
max_size = 6 * 1024 * 1024

# 读取原文件内容
with open(file_path, 'r', encoding='utf-8') as file:
    lines = file.readlines()

# 按字节计算大小，拆分内容
size = 0
file_index = 1
current_lines = []

for i, line in enumerate(lines):
    size += len(line.encode('utf-8'))  # 计算字节大小
    current_lines.append(line)

    if size > max_size:
        # 写入当前文件
        with open(f"{file_prefix}{file_index}.txt", 'w', encoding='utf-8') as file:
            file.writelines(current_lines)

        # 重置累加器和当前行列表
        size = 0
        current_lines = []
        file_index += 1

# 写入剩余内容
if current_lines:
    with open(f"{file_prefix}{file_index}.txt", 'w', encoding='utf-8') as file:
        file.writelines(current_lines)

print(f"拆分完成：共拆分为 {file_index} 个文件，每个文件不超过 6MB。")
